import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Login extends JDialog {

	//private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel1 = null;

	private JPanel jPanel13 = null;

	private JButton jButtonStart = null;

	private JButton jButtonNew = null;

	private JPanel jPanel2 = null;

	private JPanel jPanel11 = null;

	private JPanel jPanel12 = null;

	private JLabel Imagemfundo = null;

	private ImageIcon imagefundo = null;

	private JLabel jLabelLogin = null;

	private JTextField jTextFieldLogin = null;

	private JPasswordField jPasswordFieldPass = null;

	private JLabel jLabelPass = null;

	private JPanel jPanel = null;

	private JPanel jPanel3 = null;

	private LinkedList<Users> Lista_users = new LinkedList<Users>(); // lista											// useres  //  @jve:decl-index=0:

	private JPanel jPanel4 = null;

	private JPanel jPanel121 = null;

	private JLabel jLabelPass1 = null;

	private JPasswordField jPasswordFieldPass1 = null;

	private JPanel jPanel31 = null;

	private JButton jButtonADD = null;

	/**
	 * @param owner
	 */
	public Login() {
		super();
		
		ImageIcon icone = new ImageIcon("Photos\\lince2.jpg"); // adiciona
		// icone na
		// janela
		setIconImage(icone.getImage());
		
		initialize();
		
		
		try {
			LoadUsers();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(386, 530);
		this.setPreferredSize(new Dimension(0, 0));
		this.setMinimumSize(new Dimension(0, 0));
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setTitle(" Lince - WebSearch - Login ");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			Imagemfundo = new JLabel(getImagefundo());
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setPreferredSize(new Dimension(0, 0));
			jContentPane.setPreferredSize(new Dimension(278, 258));
			jContentPane.add(getJPanel4(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
			jContentPane.add(Imagemfundo, BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BorderLayout());
			jPanel1.setPreferredSize(new Dimension(0, 50));
			jPanel1.setPreferredSize(new Dimension(0, 0));
			jPanel1.add(getJPanel13(), java.awt.BorderLayout.EAST);
			jPanel1.add(getJPanel2(), java.awt.BorderLayout.CENTER);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel13
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel13() {
		if (jPanel13 == null) {
			jPanel13 = new JPanel();
			jPanel13.setLayout(new BorderLayout());
			jPanel13.setPreferredSize(new Dimension(120, 0));
			jPanel13.setBackground(new Color(10, 16, 40));
			jPanel13.add(getJPanel(), BorderLayout.NORTH);
			jPanel13.add(getJPanel3(), BorderLayout.CENTER);
		}
		return jPanel13;
	}

	/**
	 * This method initializes jButtonStart
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonStart() {
		if (jButtonStart == null) {
			jButtonStart = new JButton();
			jButtonStart.setPreferredSize(new Dimension(89, 20));
			jButtonStart.setVerticalAlignment(SwingConstants.CENTER);
			jButtonStart.setText(" START ");
			jButtonStart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					for (int i = 0; i < Lista_users.size(); i++)
						if (jTextFieldLogin.getText().compareTo(
								Lista_users.get(i).nome) == 0)
							if (jPasswordFieldPass.getText().compareTo(
									Lista_users.get(i).password) == 0){

								try {
									gravarUsers();
								} catch (FileNotFoundException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								
								Principal a = new Principal(jTextFieldLogin.getText());
								esconde(); //login menu
								a.setVisible(true);
								
							}
							else
								JOptionPane.showMessageDialog(null,
										"Wrong Password", "Error",
										JOptionPane.ERROR_MESSAGE);

				}

			});
		}
		return jButtonStart;
	}

	/**
	 * This method initializes jButtonNew
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonNew() {
		if (jButtonNew == null) {
			jButtonNew = new JButton();
			jButtonNew.setFont(new Font("Dialog", Font.BOLD, 12));
			jButtonNew.setMnemonic(KeyEvent.VK_UNDEFINED);
			jButtonNew.setText("New User");
			jButtonNew.setPreferredSize(new Dimension(89, 20));
			jButtonNew.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanel4.setVisible(true);
				}
			});
		}
		return jButtonNew;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new BorderLayout());
			jPanel2.setPreferredSize(new Dimension(0, 40));
			jPanel2.add(getJPanel11(), java.awt.BorderLayout.NORTH);
			jPanel2.add(getJPanel12(), java.awt.BorderLayout.CENTER);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel11
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel11() {
		if (jPanel11 == null) {
			jLabelLogin = new JLabel();
			jLabelLogin.setText(" Login ");
			jLabelLogin.setForeground(Color.white);
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(12);
			flowLayout.setAlignment(FlowLayout.RIGHT);
			flowLayout.setVgap(19);
			flowLayout.setVgap(12);
			jPanel11 = new JPanel();
			jPanel11.setLayout(flowLayout);
			jPanel11.setPreferredSize(new Dimension(0, 40));
			jPanel11.setLayout(flowLayout);
			jPanel11.setBackground(new Color(10, 16, 40));
			jPanel11.add(jLabelLogin, null);
			jPanel11.add(getJTextFieldLogin(), null);
		}
		return jPanel11;
	}

	/**
	 * This method initializes jPanel12
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel12() {
		if (jPanel12 == null) {
			jLabelPass = new JLabel();
			jLabelPass.setText(" Password ");
			jLabelPass.setForeground(Color.white);
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setHgap(12);
			flowLayout1.setAlignment(FlowLayout.RIGHT);
			flowLayout1.setVgap(10);
			jPanel12 = new JPanel();
			jPanel12.setPreferredSize(new Dimension(0, 40));
			jPanel12.setLayout(flowLayout1);
			jPanel12.setBackground(new Color(10, 16, 40));
			jPanel12.add(jLabelPass, null);
			jPanel12.add(getJPasswordFieldPass(), null);
		}
		return jPanel12;
	}

	/**
	 * This method initializes imagefundo
	 * 
	 * @return javax.swing.ImageIcon
	 */
	private ImageIcon getImagefundo() {
		if (imagefundo == null) {
			imagefundo = new ImageIcon("Photos\\lince.jpg");
		}
		return imagefundo;
	}

	
	//Adicionar novos utilizadores 
	private void AddUser() {
		int flag = 0;
		int flag2 = 0;

		if (jPasswordFieldPass.getText().compareTo(jPasswordFieldPass1.getText())!=0)
			flag2 = 1;
		
		for (int i = 0; i < Lista_users.size(); i++)
			if (Lista_users.get(i).nome.compareTo(jTextFieldLogin.getText()) == 0) 
					flag = 1;
		
		
		if (flag == 0 && flag2 == 0) {
			// Adiciona novo user
			Users u = new Users(jTextFieldLogin.getText(), jPasswordFieldPass.getText());
			Lista_users.add(u);
			
			//Cria pasta para novo user
			File f = new File(jTextFieldLogin.getText());
			f.mkdir();
			
			JOptionPane.showMessageDialog(null, "-- Welcome --\n-- Press START button to enter in WebSearch --");	
			
		} 
		if (flag==1 && flag2==1)
			JOptionPane.showMessageDialog(null, "--WrongPassword and Choose another nick  --\n");	
			
		else if (flag==1)
			JOptionPane.showMessageDialog(null, "-- Choose another nick  --\n");	
		
		else if (flag2==1)
			JOptionPane.showMessageDialog(null,"Wrong Password", "Error",JOptionPane.ERROR_MESSAGE);
	
	}
	/**
	 * This method initializes jTextFieldLogin
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldLogin() {
		if (jTextFieldLogin == null) {
			jTextFieldLogin = new JTextField();
			jTextFieldLogin.setPreferredSize(new Dimension(140, 20));
		}
		return jTextFieldLogin;
	}

	/**
	 * This method initializes jPasswordFieldPass
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordFieldPass() {
		if (jPasswordFieldPass == null) {
			jPasswordFieldPass = new JPasswordField();
			jPasswordFieldPass.setPreferredSize(new Dimension(140, 20));
		}
		return jPasswordFieldPass;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			FlowLayout flowLayout2 = new FlowLayout();
			flowLayout2.setVgap(15);
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout2);
			jPanel.setPreferredSize(new Dimension(0, 40));
			jPanel.setBackground(new Color(10, 16, 40));
			jPanel.add(getJButtonStart(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel3
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			FlowLayout flowLayout3 = new FlowLayout();
			flowLayout3.setVgap(10);
			jPanel3 = new JPanel();
			jPanel3.setLayout(flowLayout3);
			jPanel3.setPreferredSize(new Dimension(0, 0));
			jPanel3.setBackground(new Color(10, 16, 40));
			jPanel3.add(getJButtonNew(), null);
		}
		return jPanel3;
	}

	
	// Gravar para o ficheiro os utilizadores registados
	public void gravarUsers() throws FileNotFoundException, IOException {
		File f1 = new File("Backup_Users\\useres.dat");

		ObjectOutputStream gravar = new ObjectOutputStream(
				new FileOutputStream(f1));

		if (f1.exists())
			gravar.writeObject(Lista_users);
	}

	
	// Carrega do ficheiro os utilizadores registados
	public void LoadUsers() throws FileNotFoundException, IOException,
			ClassNotFoundException {

		File f3 = new File("Backup_Users\\useres.dat");

		if (!f3.exists())
			JOptionPane.showMessageDialog(null, "-- Create a new user --\n");
		else {
			ObjectInputStream ler = new ObjectInputStream(new FileInputStream(
					f3));

			Lista_users = (LinkedList<Users>) ler.readObject();
		}
	}

	
	//Esconde login menu
	private void esconde(){
		this.setVisible(false);
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			jPanel4 = new JPanel();
			jPanel4.setLayout(new BorderLayout());
			jPanel4.setBackground(new Color(10, 16, 40));
			jPanel4.setPreferredSize(new Dimension(0, 40));
			jPanel4.setVisible(false);
			jPanel4.add(getJPanel121(), BorderLayout.CENTER);
			jPanel4.add(getJPanel31(), BorderLayout.EAST);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jPanel121	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel121() {
		if (jPanel121 == null) {
			jLabelPass1 = new JLabel();
			jLabelPass1.setForeground(Color.white);
			jLabelPass1.setText("Confirm Pass");
			FlowLayout flowLayout11 = new FlowLayout();
			flowLayout11.setHgap(12);
			flowLayout11.setAlignment(FlowLayout.RIGHT);
			flowLayout11.setVgap(10);
			jPanel121 = new JPanel();
			jPanel121.setPreferredSize(new Dimension(0, 40));
			jPanel121.setLayout(flowLayout11);
			jPanel121.setBackground(new Color(10, 16, 40));
			jPanel121.add(jLabelPass1, null);
			jPanel121.add(getJPasswordFieldPass1(), null);
		}
		return jPanel121;
	}

	/**
	 * This method initializes jPasswordFieldPass1	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordFieldPass1() {
		if (jPasswordFieldPass1 == null) {
			jPasswordFieldPass1 = new JPasswordField();
			jPasswordFieldPass1.setPreferredSize(new Dimension(140, 20));
		}
		return jPasswordFieldPass1;
	}

	/**
	 * This method initializes jPanel31	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel31() {
		if (jPanel31 == null) {
			FlowLayout flowLayout31 = new FlowLayout();
			flowLayout31.setVgap(10);
			jPanel31 = new JPanel();
			jPanel31.setPreferredSize(new Dimension(120, 40));
			jPanel31.setLayout(flowLayout31);
			jPanel31.setBackground(new Color(10, 16, 40));
			jPanel31.add(getJButtonADD(), null);
		}
		return jPanel31;
	}

	/**
	 * This method initializes jButtonADD	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonADD() {
		if (jButtonADD == null) {
			jButtonADD = new JButton();
			jButtonADD.setFont(new Font("Dialog", Font.BOLD, 12));
			jButtonADD.setMnemonic(KeyEvent.VK_UNDEFINED);
			jButtonADD.setText("Add User");
			jButtonADD.setPreferredSize(new Dimension(89, 20));
			jButtonADD.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					AddUser();
				}
			});
		}
		return jButtonADD;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"